import redis
from constant import constant


#站台
ZT01_WD_LIST = [
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0472',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0474',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0476',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0478',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0606',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0608',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0610',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0612'
]

ZT01_SD_LIST = [
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0473',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0475',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0477',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0479',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0607',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0609',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0611',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0613'
]

ZT01_CO2_LIST = [
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0524',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0525',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0526',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0527',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0624',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0625',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0626',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0627'
]


#站厅
ZT02_WD_LIST = [
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0446',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0448',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0552',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0554'
]

ZT02_SD_LIST = [
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0447',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0449',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0553',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0555'
]

ZT02_CO2_LIST = [
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0552',
'A_SEC.A_SEC.Global.HMIAI_x.HMIAI_0553',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0622',
'B_PRI.B_PRI.Global.HMIAI_x.HMIAI_0623'
]

ZT01_WD_AVG = 'ZT01_WD_AVG'
ZT01_SD_AVG = 'ZT01_SD_AVG'
ZT01_CO2_AVG = 'DGL_ZT01_CO2_AVG'

ZT02_WD_AVG = 'ZT02_WD_AVG'
ZT02_SD_AVG = 'ZT02_SD_AVG'
ZT02_CO2_AVG = 'DGL_ZT02_CO2_AVG'

class ZT():
    '''站台站厅温湿CO2度处理'''
    def zt_01_02(self):
        # 站台
        self.zt_avg(ZT01_WD_LIST, ZT01_WD_AVG)
        self.zt_avg(ZT01_SD_LIST, ZT01_SD_AVG)
        self.zt_avg(ZT01_CO2_LIST, ZT01_CO2_AVG)
        # 站厅
        self.zt_avg(ZT02_WD_LIST, ZT02_WD_AVG)
        self.zt_avg(ZT02_SD_LIST, ZT02_SD_AVG)
        self.zt_avg(ZT02_CO2_LIST, ZT02_CO2_AVG)

    def zt_avg(self, list, key):
        pool = redis.ConnectionPool(host=constant.REDIS_HOST, port=constant.REDIS_PORT,
                                    password=constant.REDIS_PASSWORD, decode_responses=True)
        r = redis.Redis(connection_pool=pool)
        value_list = r.hmget('data_realtime', list)
        sum = 0
        i = 0
        for value in value_list:
            value = float(value) if float(value) else 0
            if value > 0:
                sum += value
                i += 1
        if i > 0:
            r.hset('data_realtime', key, round(sum / (i * 10), 1))






